﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PerfilEmpreendedor.Dao.Factory;
using PerfilEmpreendedor.Dao.Interfaces;
using Ext.Net;
using PerfilEmpreendedor.Bo.Model;
using System.Collections;

namespace PerfilEmpreendedor.FrontEnd.UserControls
{
    public partial class CadastrarCartela : System.Web.UI.UserControl
    {
        private DaoFactory daoFactory = DaoFactory.GetInstance;
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        public void CarregarTabelas()
        {
            INivelDao nivelDao = daoFactory.GetNivelDao;
            selectNivel.Store[0].DataSource = nivelDao.List("Descricao");
            selectNivel.Store[0].DataBind();

            ITipoCartelaDao tipoCartelaDao = daoFactory.GetTipoCartelaDao;
            selectTipo.Store[0].DataSource = tipoCartelaDao.List("Descricao");
            selectTipo.Store[0].DataBind();
        }
        public void Open()
        {
            Listar();
            windowGrid.Show();
        }
        protected void NovoRegistro(object sender, EventArgs e)
        {
            formCadastro.Reset();
            windowForm.Show();
            windowGrid.Hide();
            CarregarTabelas();
            CarregarGridVazio();

        }
        protected void Listar()
        {
            ICartelaDao cartelaDao = daoFactory.GetCartelaDao;
            storeDados.DataSource = cartelaDao.List();
            storeDados.DataBind();
        }
        protected void Salvar(Object sender, DirectEventArgs e)
        {
            try
            {
                ICartelaDao cartelaDao = daoFactory.GetCartelaDao;
                Cartela cartela = new Cartela();
                cartela.Id = (!String.IsNullOrEmpty(hiddenId.Text)) ? Convert.ToInt32(hiddenId.Text) : 0;
                cartela.Tipo = new TipoCartela() { Id = Convert.ToInt32(selectTipo.SelectedItem.Value) };
                cartela.Nivel = new Nivel() { Id = Convert.ToInt32(selectNivel.SelectedItem.Value) };
                cartela.Resposta = txtResposta.Text.Trim();
                cartela.Dicas = new List<Dica>();

                String rows = e.ExtraParams["gridRows"];
                Dictionary<string, string>[] records = JSON.Deserialize<Dictionary<string, string>[]>(rows);
                foreach (var row in records)
                {
                    Dica dica = new Dica();
                    dica.Cartela = cartela;
                    dica.Id = (Convert.ToInt32(row["Id"]) > 0) ? Convert.ToInt32(row["Id"]) : 0;

                    if (row["Descricao"]!=null)
                    {
                        dica.Descricao = row["Descricao"].ToString();
                    }
                    else
                    {
                        throw new Exception("Preencha todas as dicas.");
                    }
                    cartela.Dicas.Add(dica);
                }
                cartelaDao.SaveOrUpdate(cartela);
                Listar();
                windowForm.Hide();
                windowGrid.Show();
                
            }
            catch (Exception ex)
            {
                ExtFunctions.Alert(ex.Message, MessageBox.Icon.ERROR);
            }
        }
        protected void Carregar(Int32 id)
        {
            CarregarTabelas();
            ICartelaDao cartelaDao = daoFactory.GetCartelaDao;
            hiddenId.Text = id.ToString();
            Cartela cartela = cartelaDao.Find(id);
            selectTipo.SelectedItem.Value = cartela.Tipo.Id.ToString();
            selectNivel.SelectedItem.Value = cartela.Nivel.Id.ToString();
            txtResposta.Text = cartela.Resposta;
            if (cartela.Dicas.Count > 0)
            {
                storeDicas.DataSource = cartela.Dicas;
                storeDicas.DataBind();
            }
            else
            {
                CarregarGridVazio();
            }

            windowForm.Show();
            windowGrid.Hide();
        }
        protected void CarregarGridVazio()
        {
            IList<Dica> lstDica = new List<Dica>();
            for (int i = 0; i < 10; i++)
            {
                Dica d = new Dica() { Id = -i };
                lstDica.Add(d);
            }
            storeDicas.DataSource = lstDica;
            storeDicas.DataBind();
        }
        protected void Pesquisar(Object sender, DirectEventArgs e)
        {
            ICartelaDao cartelaDao = daoFactory.GetCartelaDao;
            storeDados.DataSource = cartelaDao.Pesquisar(txtPesquisa.Text.Trim());
            storeDados.DataBind();
        }
        protected void OnGridCommand(Object sender, DirectEventArgs e)
        {
            Carregar(Convert.ToInt32(e.ExtraParams["Id"]));
        }
    }

}
